home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks96 / InternetChooser.sit / Internet Chooser / main.cp < prev    next >
Text File  |  1996-06-22  |  12KB  |  447 lines

  1. #include <string.h>
  2. #include <ctype.h>
  3.  
  4. #include "stdtypes.h"
  5. #include "stddebug.h"
  6.  
  7. #include "client.h"
  8. #include "datagram.h"
  9. #include "platform.h"
  10. #include "reggie.h"
  11. #include "socket.h"
  12.  
  13. #include "ICAPI.h"
  14. #include "main.h"
  15.  
  16. // Globals
  17. Str255    gInfo = "\pThe Internet Chooser by Matt and Mike!";
  18. DialogPtr gChooserDialogPtr;
  19. Boolean   gQuitting;
  20. ListHandle gServiceList;
  21. ListHandle gURLList;
  22. short gFilterValue;
  23. extern RegEntry gRegStore[kRegEntryCount];
  24. extern UInt32 gRegCount;
  25. ICInstance    gICInst;
  26. CursHandle gWatch = 0;
  27.  
  28. // * *******************************************************************************
  29. // * *******************************************************************************
  30.  
  31. void main(void) {
  32.     OSErr err = noErr;
  33.     
  34.     Initialize();
  35.     SetUpMenus();
  36.     if(SetUpChooser())
  37.         while(!gQuitting)
  38.             err = EventLoop();
  39.     DisposDialog(gChooserDialogPtr);
  40.     }
  41.     
  42. // * *******************************************************************************
  43. // * *******************************************************************************
  44.  
  45. void SetUpMenus(void) {
  46.     InsertMenu(GetMenu(128), 0);
  47.     AddResMenu(GetMHandle(128), 'DRVR');
  48.     
  49.     InsertMenu(GetMenu(129), 0);
  50.     InsertMenu(GetMenu(130), 0);
  51.     
  52.     DisableItem(GetMenu(130), 1);
  53.     DisableItem(GetMenu(130), 2);
  54.     DisableItem(GetMenu(130), 3);
  55.     DisableItem(GetMenu(130), 4);
  56.     
  57.     DrawMenuBar();
  58.     
  59.     }
  60.     
  61. // * *******************************************************************************
  62. // * *******************************************************************************
  63.  
  64. DialogPtr SetUpChooser(void) {
  65.     Rect ServiceRect, URLRect, InfoRect, ServiceListBounds, URLListBounds;
  66.     short iType, i=0, j=-1, row, total=0;
  67.     Handle iHandle, aServiceIcon;
  68.     Point serviceCellSize, URLCellSize;
  69.     Ptr cellData;
  70.     StringHandle iconString;
  71.     Cell currentCell;
  72.     OSErr err=noErr;
  73.     GrafPtr oldPort;
  74.     
  75.     GetPort(&oldPort);
  76.     
  77.     if(gChooserDialogPtr = GetNewDialog(128, NULL, (WindowPtr)-1)) {
  78.         SetPort(gChooserDialogPtr);
  79.         
  80.         GetDItem(gChooserDialogPtr, kServiceList, &iType, &iHandle, &ServiceRect);
  81.         GetDItem(gChooserDialogPtr, kURLList, &iType, &iHandle, &URLRect);        
  82.         
  83.         SetRect(&ServiceListBounds, 0, 0, 3, 3);
  84.         SetPt(&serviceCellSize, 60, 60);
  85.         gServiceList = LNew(&ServiceRect, &ServiceListBounds, serviceCellSize, 130, gChooserDialogPtr,
  86.             TRUE, FALSE, FALSE, TRUE);
  87.         
  88.         SetRect(&URLListBounds, 0, 0, 1, 10);
  89.         SetPt(&URLCellSize, 0, 0);
  90.         gURLList = LNew(&URLRect, &URLListBounds, URLCellSize, 0, gChooserDialogPtr,
  91.             TRUE, FALSE, FALSE, TRUE);
  92.         
  93.         
  94.         cellData = NewPtr(sizeof(char)*64);
  95.         if(MemError())
  96.             return(0);
  97.         i=128;
  98.         total = Count1Resources('STR ');
  99.         for(i=0; (i*3)+j<total; i++, j=0) {
  100.             for(j=0; (j<3) && ((i*3)+j<total);j++) {
  101.                 GetIconSuite(&aServiceIcon, (i*3)+j+128, svAllAvailableData);
  102.  
  103.                 iconString = GetString((i*3)+j+128);
  104.                 if(!iconString)
  105.                     break;
  106.                     
  107.                 SetPt((Point *)(¤tCell), j, i);
  108.                 BlockMove(&aServiceIcon, cellData, sizeof(Handle));
  109.                 BlockMove((*iconString)+1, cellData+sizeof(Handle), (*iconString)[0]);
  110.                 LSetCell(cellData, (*iconString)[0]+sizeof(Handle), currentCell, gServiceList);
  111.                 
  112.                 ReleaseResource((Handle)iconString);
  113.                 if(ResError())
  114.                     return(0);
  115.                 }
  116.             }
  117.         ShowWindow((WindowPtr)gChooserDialogPtr);
  118.         SetPort(oldPort);
  119.         }
  120.         
  121.     DisposePtr(cellData);
  122.     return(gChooserDialogPtr);
  123.     }
  124.     
  125. // * *******************************************************************************
  126. // * *******************************************************************************
  127.  
  128. //Initialize the ToolBox
  129. void Initialize(void) {
  130.     WindowPtr    mainPtr;
  131.     OSErr        error;
  132.     SysEnvRec    theWorld;
  133.         
  134.     gQuitting = FALSE;
  135.     error = SysEnvirons(1, &theWorld);
  136.     if (theWorld.hasColorQD == false) {
  137.         SysBeep(50);
  138.         ExitToShell();                    /* If no color QD, we must leave. */
  139.         }
  140.     
  141.     /* Initialize all the needed managers. */
  142.     InitGraf(&qd.thePort);
  143.     InitFonts();
  144.     InitWindows();
  145.     InitMenus();
  146.     TEInit();
  147.     InitDialogs(nil);
  148.     InitCursor();
  149.     
  150.     MoreMasters();
  151.     MoreMasters();
  152.     MoreMasters();
  153.     
  154.     gWatch = GetCursor(watchCursor);
  155.  
  156.     ICStart(&gICInst, 'MSMM');
  157.     ICFindConfigFile(gICInst, 0, NULL);
  158.     }
  159.     
  160.  
  161. // * *******************************************************************************
  162. // * *******************************************************************************
  163.  
  164. void ChangeURLList(short index) {    
  165.     short i=0, iType;
  166.     Cell curCell;
  167.     Rect iRect;
  168.     Handle iHandle;
  169.     RgnHandle infoRgn = NewRgn();
  170.     StringHandle ourString;
  171.     char state;
  172.     Str32 typeMask;
  173.     
  174.     ourString = GetString(128+index);
  175.     for(i=1;i<=((*ourString)[0]);i++)
  176.         (*ourString)[i] = tolower((*ourString)[i]);
  177.     
  178.     BlockMove((*ourString)+1, typeMask, (*ourString)[0]);
  179.     typeMask[(*ourString)[0]] = 0;
  180.     state = HGetState((Handle)ourString);    
  181.     HLock((Handle)ourString);
  182.     QueryTracker(0x12345678, 0, 100, (char *)typeMask, NULL, NULL, 
  183.             "sils.umich.edu");
  184.     HSetState((Handle)ourString, state);
  185.  
  186.     LDelRow(0, 0, gURLList);
  187.  
  188.     GetDItem(gChooserDialogPtr, kInfoField, &iType, &iHandle, &iRect);
  189.     RectRgn(infoRgn, &iRect);
  190.     InvalRgn(infoRgn);
  191.     BlockMove("\p", gInfo, 1);
  192.     
  193.     for(i=0;i<gRegCount;i++) {
  194.         LAddRow(1, i, gURLList); 
  195.         SetPt(&curCell, 0, i); 
  196.         LSetCell(gRegStore[i].serviceName, strlen(gRegStore[i].serviceName), curCell, gURLList); 
  197.         }
  198.         
  199.     ReleaseResource((Handle)ourString);
  200.     DisposeRgn(infoRgn);
  201.     }
  202.  
  203. // * *******************************************************************************
  204. // * *******************************************************************************
  205.  
  206. void DoMouseInDialog(EventRecord theEvent, short theItem) {
  207.     long result=0;
  208.     GrafPtr oldPort;
  209.     Handle filterPopUp;
  210.     short iType;
  211.     Rect iRect;
  212.     RgnHandle infoRgn = NewRgn();
  213.     Point localPt;
  214.     Cell ourCell;
  215.     long length, start=0;
  216.     
  217.     GetPort(&oldPort);
  218.     SetPort((GrafPtr)gChooserDialogPtr);
  219.  
  220.     localPt = theEvent.where;
  221.     GlobalToLocal(&localPt);
  222.     switch(theItem) {
  223.         case kServiceList:
  224.         case kServiceListScroll:
  225.             if (gWatch) SetCursor(*gWatch);
  226.             LClick(localPt, theEvent.modifiers, gServiceList);
  227.             SetPt(&ourCell, 0, 0);
  228.             LGetSelect(TRUE, &ourCell, gServiceList);
  229.             ChangeURLList((ourCell.v*3)+ourCell.h);
  230.             break;
  231.         case kURLList:
  232.         case kURLListScroll:
  233.             SetPt(&ourCell, 0, 0);
  234.             if(LClick(localPt, theEvent.modifiers, gURLList)) {
  235.                 if (gWatch) SetCursor(*gWatch);
  236.                 LGetSelect(TRUE, &ourCell, gURLList);
  237.                 length = strlen(gRegStore[ourCell.v].serviceName);
  238.                     if(result = ICLaunchURL(gICInst, "\p", 
  239.                             gRegStore[ourCell.v].serviceName,
  240.                             length, &start, &length)) SysBeep(4);
  241.                 }
  242.             if(LGetSelect(TRUE, &ourCell, gURLList)) {
  243.                 BlockMove(gRegStore[ourCell.v].serviceInfo, gInfo+1,
  244.                     gInfo[0]=strlen(gRegStore[ourCell.v].serviceInfo));
  245.                 GetDItem(gChooserDialogPtr, kInfoField, &iType, &filterPopUp, &iRect);
  246.                 RectRgn(infoRgn, &iRect);
  247.                 InvalRgn(infoRgn);
  248.                 }
  249.             else {
  250.                 BlockMove("\p", gInfo, 1);
  251.                 GetDItem(gChooserDialogPtr, kInfoField, &iType, &filterPopUp, &iRect);
  252.                 RectRgn(infoRgn, &iRect);
  253.                 InvalRgn(infoRgn);
  254.                 }
  255.             break;
  256.         case kFilterMenu:
  257.             GetDItem(gChooserDialogPtr, kFilterMenu, &iType, &filterPopUp, &iRect);
  258.             gFilterValue = GetCtlValue((ControlHandle)filterPopUp);
  259.             GetDItem(gChooserDialogPtr, kInfoField, &iType, &filterPopUp, &iRect);
  260.             RectRgn(infoRgn, &iRect);
  261.             InvalRgn(infoRgn);
  262.         default:
  263.             break;
  264.         }
  265.     
  266.     DisposeRgn(infoRgn);
  267.     SetPort(oldPort);
  268.     }
  269.     
  270. // * *******************************************************************************
  271. // * *******************************************************************************
  272.  
  273. void DoMouseOutsideDialog(EventRecord theEvent, short theItem) {
  274.     short winPart;
  275.     GrafPtr oldPort;
  276.     long menuStuff;
  277.     WindowPtr testWindow;
  278.     
  279.     GetPort(&oldPort);
  280.     SetPort((GrafPtr)gChooserDialogPtr);
  281.     
  282.     winPart = FindWindow(theEvent.where, &testWindow);
  283.     
  284.     switch(winPart) {
  285.         case inMenuBar:
  286.             menuStuff = MenuSelect(theEvent.where);
  287.             if(menuStuff)
  288.                 DoMenu(HiWord(menuStuff), LoWord(menuStuff));
  289.             break;
  290.         case inDrag:
  291.             DragWindow((WindowPtr)gChooserDialogPtr, theEvent.where, &(qd.screenBits.bounds));
  292.             break;
  293.         case inGoAway:
  294.             if(TrackGoAway(gChooserDialogPtr, theEvent.where))
  295.                 gQuitting = TRUE;
  296.             break;
  297.         default:
  298.             break;
  299.         }
  300.     SetPort(oldPort);
  301.     }
  302.  
  303. // * *******************************************************************************
  304. // * *******************************************************************************
  305.  
  306. void DoKey(EventRecord theEvent, short theItem) {
  307.     GrafPtr oldPort;
  308.  
  309.     GetPort(&oldPort);
  310.     SetPort((GrafPtr)gChooserDialogPtr);
  311.     
  312.     
  313.     //Do Stuff...
  314.     
  315.     
  316.     SetPort(oldPort);
  317.     }
  318.     
  319. // * *******************************************************************************
  320. // * *******************************************************************************
  321.  
  322. void DoMenu(short menuID, short menuItem) {
  323.     GrafPtr oldPort;
  324.     Str255 DAName;
  325.     
  326.     GetPort(&oldPort);
  327.     SetPort((GrafPtr)gChooserDialogPtr);
  328.     
  329.     if((menuID == 0) && (menuItem < 0)) {
  330.         GetItem(GetMHandle(128), menuItem, DAName);
  331.         OpenDeskAcc(DAName);
  332.         }
  333.     
  334.     if(menuID == 129) {        //File Menu...
  335.         gQuitting = TRUE;
  336.         }
  337.     
  338.     if(menuID == 130) {        //Edit Menu...
  339.         //Everything is disabled...
  340.         }
  341.  
  342.     HiliteMenu(0);
  343.         
  344.     SetPort(oldPort);
  345.     }
  346.     
  347. // * *******************************************************************************
  348. // * *******************************************************************************
  349.  
  350. OSErr EventLoop(void) {
  351.     DialogPtr myDlg;
  352.     short theItem;
  353.     EventRecord theEvent;
  354.     Boolean handled = FALSE, inDialog = TRUE;
  355.     long menuStuff;
  356.     GrafPtr oldPort;
  357.     
  358.     if(WaitNextEvent(everyEvent, &theEvent, 30, NULL)) {
  359.         GetPort(&oldPort);
  360.         SetPort(gChooserDialogPtr);
  361.         TextFont(0);
  362.         TextSize(0);
  363.         SetPort(oldPort);
  364.         if ((theEvent.what != updateEvt) && (inDialog=IsDialogEvent(&theEvent)))
  365.                 handled = !DialogSelect(&theEvent, &myDlg, &theItem);
  366.         GetPort(&oldPort);
  367.         SetPort(gChooserDialogPtr);
  368.         TextFont(1);
  369.         TextSize(9);
  370.         SetPort(oldPort);
  371.  
  372.         if(handled == FALSE) {
  373.             switch(theEvent.what) {
  374.                 case mouseDown:
  375.                     if(inDialog)
  376.                         DoMouseInDialog(theEvent, theItem);
  377.                     else
  378.                         DoMouseOutsideDialog(theEvent, theItem);
  379.                     break;
  380.                 case keyDown:
  381.                     DoKey(theEvent, theItem);
  382.                     break;
  383.                 case updateEvt:
  384.                     DoUpdate();
  385.                     break;
  386.                 case nullEvent:
  387.                 default:
  388.                     break;
  389.                 }
  390.             }
  391.         else {
  392.             if((theEvent.modifiers & cmdKey)&&(theEvent.what == keyDown))
  393.                 menuStuff = MenuKey(theEvent.message & charCodeMask);
  394.                 if(menuStuff) {
  395.                     HiliteMenu(HiWord(menuStuff));
  396.                     DoMenu(HiWord(menuStuff), LoWord(menuStuff));
  397.                     }
  398.             }
  399.         
  400.         SetCursor(&qd.arrow);
  401.         }
  402.     return noErr;
  403.     }
  404.     
  405. // * *******************************************************************************
  406. // * *******************************************************************************
  407.  
  408. void DoUpdate(void) {
  409.     GrafPtr oldPort;    
  410.     Rect ServiceRect, URLRect, InfoRect;
  411.     short iType;
  412.     Handle iHandle;
  413.  
  414.     GetPort(&oldPort);
  415.     SetPort(gChooserDialogPtr);
  416.     
  417.     GetDItem(gChooserDialogPtr, kServiceList, &iType, &iHandle, &ServiceRect);
  418.     GetDItem(gChooserDialogPtr, kURLList, &iType, &iHandle, &URLRect);
  419.     GetDItem(gChooserDialogPtr, kInfoField, &iType, &iHandle, &InfoRect);
  420.     
  421.     BeginUpdate(gChooserDialogPtr);
  422.  
  423.     UpdtDialog(gChooserDialogPtr, gChooserDialogPtr->visRgn);
  424.     
  425.     InsetRect(&ServiceRect, -1, -1);
  426.     InsetRect(&URLRect, -1, -1);
  427.  
  428.     FrameRect(&ServiceRect);
  429.     FrameRect(&URLRect);
  430.     
  431.     PenPat(&qd.gray);
  432.     FrameRect(&InfoRect);
  433.     PenPat(&qd.black);
  434.     
  435.     LUpdate((*gServiceList)->port->visRgn, gServiceList);
  436.     LUpdate((*gURLList)->port->visRgn, gURLList);
  437.  
  438.     InsetRect(&InfoRect, 2, 2);
  439.     
  440.     //NumToString(gFilterValue, gInfo);
  441.     TextBox(&(gInfo[1]), gInfo[0], &InfoRect, 0);
  442.     
  443.     EndUpdate(gChooserDialogPtr);
  444.  
  445.     SetPort(oldPort);
  446.     }
  447.